package com.hd.batch.quartz.common.config;

import com.hd.batch.quartz.batch.job.config.DemoJob;
import com.hd.batch.quartz.detail.job.DemoQuartzJob;
import org.quartz.JobDataMap;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * <h3>partitionjob</h3>
 * <p>QuartzConfig</p>
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2020-09-10 16:26
 **/
@Configuration
public class QuartzConfig {

    @Autowired
    @Qualifier("runDemoJob")
    private Job runDemoJob;

    @Autowired
    @Qualifier("runDemoJob2")
    private Job runDemoJob2;

    @Autowired
    @Qualifier("jobLauncher")
    private SimpleJobLauncher jobLauncher;


    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean() throws Exception {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("job", runDemoJob);
        jobDataMap.put("jobLauncher", jobLauncher);
        jobDetailFactoryBean.setJobDataMap(jobDataMap);
        jobDetailFactoryBean.setJobClass(DemoQuartzJob.class);
        return jobDetailFactoryBean;
    }

    /**
     * 注册 多个任务配置
     * 可以用一个 执行任务bean
     * @return
     * @throws Exception
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws Exception {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean().getObject(),
                cronTriggerFactoryBean2().getObject());
        return schedulerFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean() throws Exception {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setJobDetail(jobDetailFactoryBean().getObject());
        triggerFactoryBean.setCronExpression("0/10 * * * * ?");
        return triggerFactoryBean;
    }

    //*************************任务2配置
    //现在将 该任务 改造成 数据库中进行配置 : 且可以进行修改
    /*
    分析:
    1: 更新定时执行表达式
        表 : 且有这个字段
        且修改完后能够重启生效
    2:
     */
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean2() throws Exception {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setJobDetail(jobDetailFactoryBean2().getObject());
        triggerFactoryBean.setCronExpression("0/15 * * * * ?");
        return triggerFactoryBean;
    }

    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean2() throws Exception {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("job", runDemoJob2);
        jobDataMap.put("jobLauncher", jobLauncher);
        jobDetailFactoryBean.setJobDataMap(jobDataMap);
        jobDetailFactoryBean.setJobClass(DemoQuartzJob.class);
        return jobDetailFactoryBean;
    }
}
